import 'package:dio/dio.dart';
import 'package:get/get.dart';
import 'package:hygf/page/home/allnet/un_net/state.dart';

import '../../../../../util/HDNavigator.dart';
import '../../../../bean/CustomerList.dart';
import '../../../../config/ApiConfig.dart';
import '../../../../http/DioClient.dart';
import '../../../../http/core/ResponseData.dart';
import '../../../../util/LoadingUtils.dart';

class UnNetLogic extends GetxController {
  final UnNetState state = UnNetState();

  bool isRefresh = false;

  @override
  void onReady() {
    super.onReady();
    LoadingUtils.loading();
    requestList(true);
  }

  void callRefresh(){
    state.controller.callRefresh();
  }

  /// 列表刷新，isRefresh：true是刷新，false加载更多
  void requestList(bool isRefresh,{String? selectedType,String? searchKey}) async {
    /// 如果是刷新，初始化参数
    if (isRefresh) {
      state.pageNum = 1;
      state.noMore = false;
      state.controller.resetLoadState();
    }

    Map<String, dynamic> data = {
      "pageNum": state.pageNum,
      "pageSize": state.pageSize,
      "statusList": [19,20,22]
    };
    if(searchKey != null){
      data["nameOrNumber"] = searchKey;
    }


    ResponseData responseData = await DioClient().doPost(
        ApiConfig.customerPage,
        data: data,
        options: Options(extra: {"hideLoading": true}));
    LoadingUtils.dismiss();
    if (responseData.isok) {
      dynamic result = Customerlist.fromJson(responseData.data);
      if (isRefresh) {
        state.dataList.clear();
        state.controller.finishRefresh();
      }
      state.dataList.addAll(result.list ?? []);
      state.noMore = !(state.pageNum * state.pageSize < (result.total ?? 0));
      state.pageNum++;
      state.controller.finishLoad(noMore: state.noMore);
      update();
    } else {
      state.controller.finishRefresh();
      state.noMore = true;
      update();
    }
  }

  closePage() {
    LoadingUtils.dismiss();
    HDNavigator.close();
  }

  @override
  void onClose() {
    LoadingUtils.dismiss();
    state.controller.dispose();
    super.onClose();
  }
}
